package org.ggn.scoreanalyze.view;

/**
 * 柱形显示框，用于显示数据的柱形
 * Created by guin_guo on 2015/12/14.
 */

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.swing.*;
import java.awt.*;

public class BarFrame extends JFrame {
    public BarFrame(String name, Number yx,Number lh,Number zd,Number jg,Number bjg) {
        BarChart barChart = new BarChart(yx,lh,zd,jg,bjg);
        add(barChart.getChartPanel());
        setTitle(name + " 柱形图");
        setSize(800, 500);
        setLocation(200, 80);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    class BarChart{
        ChartPanel frame1;
        public BarChart(Number yx, Number lh, Number zd, Number jg, Number bjg){
            CategoryDataset data = getDataSet(yx,lh,zd,jg,bjg);
            JFreeChart chart = ChartFactory.createBarChart3D(
                    "柱形图数据", // 图表标题
                    "成绩区间", // 目录轴的显示标签
                    "人数", // 数值轴的显示标签
                    data, // 数据集
                    PlotOrientation.VERTICAL, // 图表方向：水平、垂直
                    true,           // 是否显示图例(对于简单的柱状图必须是false)
                    false,          // 是否生成工具
                    false           // 是否生成URL链接
            );

            //从这里开始
            CategoryPlot plot=chart.getCategoryPlot();//获取图表区域对象
            CategoryAxis domainAxis=plot.getDomainAxis();         //水平底部列表
            domainAxis.setLabelFont(new Font("黑体", Font.BOLD,14));         //水平底部标题
            domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD,12));  //垂直标题
            ValueAxis rangeAxis=plot.getRangeAxis();//获取柱状
            rangeAxis.setAutoTickUnitSelection(false);//只显示整数标签
            rangeAxis.setLabelFont(new Font("黑体", Font.BOLD,15));
            chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 15));
            chart.getTitle().setFont(new Font("宋体", Font.BOLD,20));//设置标题字体

            //到这里结束，解决汉字乱码问题

            frame1=new ChartPanel(chart,true);        //这里也可以用chartFrame,可以直接生成一个独立的Frame

        }
        public ChartPanel getChartPanel(){
            return frame1;
        }
    }

    private static CategoryDataset getDataSet(Number yx,Number lh,Number zd,Number jg,Number bjg) {
        DefaultCategoryDataset data = new DefaultCategoryDataset();
        data.addValue(yx, "优秀", "90-100");
        data.addValue(lh, "良好", "80-90");
        data.addValue(zd, "中等", "70-80");
        data.addValue(jg, "及格", "60-70");
        data.addValue(bjg, "不及格", "0-60");
        return data;
    }

}
